import instance from "../utils/request"

//一般情况下，接口类型会放到一个文件
// 下面两个TS接口，表示要传的参数

interface ReqQueryGoods {
    size: number,
    total: number,
    page: number,
    searchValue: string,
}

interface ReqAddGoods {
    title: string,
    price: number,
    content: string,
    picture: string,
    userId: string,
    type: string,
}


// Res是返回的参数，T是泛型，需要自己定义，返回对数统一管理***
type Res<T> = Promise<ItypeAPI<T>>;
// 一般情况下响应数据返回的这三个参数，
// 但不排除后端返回其它的可能性，
interface ItypeAPI<T> {
    data: T,//请求的数据，用泛型
    msg: string | null // 返回状态码的信息，如请求成功等
    code: number //返回后端自定义的200，404，500这种状态码
}

//分页查询orders，模糊查询
export const QueryOrdersAPI = (data: ReqQueryGoods): Res<any> =>
    instance.post("/order/queryOrders", data);

//查询order详情
export const GetOrderDetailsAPI = (params: { orderId: string }): Res<any> =>
    instance.get("/order/getOrderDetails",{ params });

//查询热门商品
export const GetHotGoodsByLimitAPI = (params: { limit: number }): Res<any> =>
    instance.get("/order/getHotGoodsByLimit",{params});

//添加商品
export const AddOrderAPI = (data: ReqAddGoods): Res<any> =>{
    const config = {
        headers: {
          // 添加请求头
          "Authorization": "Bearer "+localStorage.getItem("x-auth-token"),
          "Content-Type": "application/json"
        }
      };
    
      return instance.post("/order/addOrder", data, config);
}


//分页查询求orders，模糊查询
export const QueryGoodsListAPI = (data: ReqQueryGoods): Res<any> =>
    instance.post("/order/queryGoodsList", data);

//分页查询供orders，模糊查询
export const QueryPurchaseListAPI = (data: ReqQueryGoods): Res<any> =>
    instance.post("/order/queryPurchaseList", data);